package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.tika.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.cmc.cloud.cmclink.bdt.api.base.PartnerDataApi;
import com.cmc.cloud.cmclink.bdt.api.base.PortApi;
import com.cmc.cloud.cmclink.bdt.api.base.VesselDataApi;
import com.cmc.cloud.cmclink.bdt.api.base.dto.req.VesselNameValidateReqDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.PartnerDataRespDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.PartnerListByCodeReqDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.PortListByCodeRespDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.VesselNameValidateRespDTO;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ApiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.BasicDataService;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;

import lombok.extern.slf4j.Slf4j;

/**
 * 箱型基础信息
 * 
 * @author dev89
 * @date 2025/08/05 13:37
 **/
@Service
@Validated
@Slf4j
public class BasicDataServiceImpl implements BasicDataService {
    @Resource
    private VesselDataApi vesselDataApi;

    @Resource
    private PortApi portApi;

    @Resource
    private PartnerDataApi partnerDataApi;

    @Override
    public String getVesselCode(String name, String partnerCode) {
        VesselNameValidateReqDTO req = new VesselNameValidateReqDTO();
        req.setVesselName(name);
        req.setVesselOperator(partnerCode);
        List<VesselNameValidateReqDTO> reqList = new ArrayList<>();
        reqList.add(req);
        CommonResult<List<VesselNameValidateRespDTO>> commonResult;
        try {
            commonResult = vesselDataApi.validateAndGetCode(reqList);
        } catch (Exception e) {
            log.error("caught an exception on getVesselCode", e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.GET_VESSEL_CODE_EXCEPTION);
        }

        if (commonResult == null || CollectionUtils.isEmpty(commonResult.getData())) {
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.GET_VESSEL_CODE_EMPTY);
        }
        return commonResult.getData().get(0).getVesselCode();
    }

    @Override
    public List<PortListByCodeRespDTO> getAllPorts() {
        CommonResult<List<PortListByCodeRespDTO>> commonResult;
        try {
            commonResult = portApi.listByPorts();
        } catch (Exception e) {
            log.error("caught an exception on getVesselCode", e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.GET_VESSEL_CODE_EXCEPTION);
        }

        if (commonResult == null || CollectionUtils.isEmpty(commonResult.getData())) {
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.GET_VESSEL_CODE_EMPTY);
        }
        return commonResult.getData();
    }

    @Override
    public PartnerDataRespDTO getPartnerByCode(String partnerCode) {
        if (StringUtils.isEmpty(partnerCode)) {
            return null;
        }
        List<String> partnerCodeList = new ArrayList<>();
        partnerCodeList.add(partnerCode);
        PartnerListByCodeReqDTO reqDTO = new PartnerListByCodeReqDTO();
        reqDTO.setPartnerCodeList(partnerCodeList);
        CommonResult<List<PartnerDataRespDTO>> commonResult;
        try {
            commonResult = partnerDataApi.listByCode(reqDTO);
        } catch (Exception e) {
            log.error("caught an exception on getVesselCode", e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_PARTNER_EXCEPTION);
        }

        if (commonResult == null || CollectionUtils.isEmpty(commonResult.getData())) {
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_PARTNER_FAILED);
        }
        return commonResult.getData().get(0);
    }

    @Override
    public List<PartnerDataRespDTO> getPartnerByCodeList(List<String> codeList) {
        if (CollectionUtils.isEmpty(codeList)) {
            return new ArrayList<>();
        }
        PartnerListByCodeReqDTO reqDTO = new PartnerListByCodeReqDTO();
        reqDTO.setPartnerCodeList(codeList);
        CommonResult<List<PartnerDataRespDTO>> commonResult;
        try {
            commonResult = partnerDataApi.listByCode(reqDTO);
        } catch (Exception e) {
            log.error("caught an exception on getVesselCode", e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_PARTNER_EXCEPTION);
        }

        if (commonResult == null) {
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_PARTNER_FAILED);
        }
        if (commonResult.getData() == null) {
            return new ArrayList<>();
        }
        return commonResult.getData();
    }
}
